package com.baibei.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.InputFilter;
import android.text.InputType;
import android.util.AttributeSet;
import android.util.TypedValue;

/**
 * 支付输入框
 * Created by ChenRui on 2017/5/15 0015 11:32.
 */
public class PaymentEditText extends android.support.v7.widget.AppCompatEditText {

    public int getMaxLength() {
        return mPasswordLength;
    }

    public interface OnTextFinishListener {
        void onTextFinish(CharSequence text, int length);
    }

    /**
     * 密码长度
     */
    private int mPasswordLength = 6;


    /**
     * 间隔
     */
    private int mSpan = 17;
    /**
     * 密码大小
     */
    private int mPasswordRadius = 10;

    // 线的高度
    private int mLineHeight = 3;


    /**
     * 密码画笔
     */
    private Paint mPwdPaint;

    /**
     * 密码框画笔
     */
    private Paint mRectPaint;
    /**
     * 输入的密码长度
     */
    private int mInputLength;

    private boolean mShowText = false;


    /**
     * 输入结束监听
     */
    private OnTextFinishListener mOnInputFinishListener;

    private final Paint mPaint = new Paint();

    private final Rect mRect = new Rect();

    /**
     * 构造方法
     *
     * @param context
     * @param attrs
     */
    public PaymentEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        setMaxLength(mPasswordLength);
        setLongClickable(false);

        mSpan = px(mSpan);
        mPasswordRadius = (int) getTextSize() / 5;
        mLineHeight = px(mLineHeight);


        // 初始化密码画笔
        mPwdPaint = new Paint();
        mPwdPaint.setColor(getTextColors().getDefaultColor());
        mPwdPaint.setStyle(Paint.Style.FILL);
        mPwdPaint.setAntiAlias(true);

        // 初始化密码框
        mRectPaint = new Paint();
        mRectPaint.setStyle(Paint.Style.FILL);
        mRectPaint.setColor(getTextColors().getDefaultColor());
        mRectPaint.setAntiAlias(true);


        setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);

    }

    @Override
    public boolean onTextContextMenuItem(int id) {
        return false;
    }

    private int px(int size) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, size, getResources().getDisplayMetrics());
    }

    /**
     * 设置最大长度
     *
     * @param maxLength
     */
    private void setMaxLength(int maxLength) {
        if (maxLength >= 0) {
            setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
        } else {
            setFilters(new InputFilter[0]);
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();

        // 覆盖系统画的
        mRect.left = 0;
        mRect.top = 0;
        mRect.right = width;
        mRect.bottom = height;
        getBackground().setBounds(mRect);
        getBackground().draw(canvas);

        // 计算每个密码框宽度
        int rectWidth = (width - mSpan * (mPasswordLength - 1)) / mPasswordLength;


        // 绘制密码框
        //2017-8-8 新需求需要在密码下面画线
        mRectPaint.setColor(Color.parseColor("#CCCCCC"));
        for (int i = 0; i < mPasswordLength; i++) {
//            if (i < mInputLength) continue;
            mRect.left = (rectWidth + mSpan) * i;
            mRect.top = (height / 2) - (mLineHeight / 2) + 50;
            mRect.right = mRect.left + rectWidth;
            mRect.bottom = (height / 2) + (mLineHeight / 2) + 50;
            canvas.drawRect(mRect, mRectPaint);
        }

        //设置颜色
        mRectPaint.setColor(getTextColors().getDefaultColor());
        getPaint().setColor(getTextColors().getDefaultColor());
        // 显示密码
        if (mShowText) {
            // 绘制文本
            for (int i = 0; i < mInputLength; i++) {
                String text = String.valueOf(getText().charAt(i));
                getPaint().getTextBounds(text, 0, 1, mRect);
                int cx = rectWidth / 2 + (rectWidth + mSpan) * i - (mRect.width() / 2);
                int cy = height / 2 + mRect.height() / 2;
                canvas.drawText(text, cx, cy, getPaint());
            }
        } else {
            // 绘制密码
            for (int i = 0; i < mInputLength; i++) {
                int cx = rectWidth / 2 + (rectWidth + mSpan) * i;
                int cy = height / 2;
                canvas.drawCircle(cx, cy, mPasswordRadius, mPwdPaint);
            }
        }
    }

    @Override
    protected void onTextChanged(CharSequence text, int start,
                                 int lengthBefore, int lengthAfter) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter);
        this.mInputLength = text.toString().length();
        invalidate();
        if (mInputLength == mPasswordLength && mOnInputFinishListener != null) {
            mOnInputFinishListener.onTextFinish(text, text.length());
        }
    }

    /**
     * 设置输入完成监听
     *
     * @param onInputFinishListener
     */
    public void setOnTextFinishListener(
            OnTextFinishListener onInputFinishListener) {
        this.mOnInputFinishListener = onInputFinishListener;
    }


    /**
     * 显示为文本
     */
    public void showText() {
        mShowText = true;
        invalidate();
    }

    /**
     * 显示为密码
     */
    public void showPassword() {
        mShowText = false;
        invalidate();
    }

//    public int getInputType() {
//        if (!mShowText) {
//            return InputType.TYPE_NUMBER_VARIATION_PASSWORD;
//        }
//
//        return super.getInputType();
//    }

}
